
/**
 * 详情对话框
 */
var OrderInfoDlg = {
    data: {
        orderNo: "",
        playerId: "",
        totalPrice: "",
        totalGoodsPrice: "",
        originalTotalGoodsPrice: "",
        payPrice: "",
        payScore: "",
        payScorePrice: "",
        deductionPrice: "",
        shipFee: "",
        receiverName: "",
        receiverPhone: "",
        receiverAddr: "",
        orderStatus: "",
        payStatus: "",
        payType: "",
        shipCompany: "",
        shipNo: "",
        remark: "",
        agentId: "",
        deptid: "",
        payTime: "",
        shipTime: "",
        receiveTime: "",
        handlePerson: "",
        createTime: "",
        updateTime: "",
        recordStatus: ""
    },


    tableId: "orderGoodsTable",


};

layui.use(['form', 'admin', 'ax','laydate','table','element'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var table = layui.table;
    var element = layui.element;
    /**
     * 初始化表格的列
     */
    OrderInfoDlg.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true,hide:'true', title: 'id'},
            {field: 'goodsName'
                , sort: true, align:'center', title: '商品名',
                event:'goodsDetail',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.goodsName +'</span>'
                }
            },

            {field: 'skuCount', sort: true, align:'center', title: '数量'},
            {field: 'goodsId', sort: true,hide:true, align:'center', title: '商品id'},
            {field: 'skuName', sort: true,  align:'center',title: '规格'},
            {field: 'skuPrice', sort: true, align:'center', title: '商品价格'},
            {field: 'skuTotalPrice', sort: true, align:'center', title: '商品总价格'},
            {align: 'center',hide: true, toolbar: '#tableBar', title: '操作'}



        ]];
    };
    var isAdmin = $("#isAdmin").val();
    //让当前iframe弹层高度适应
    //admin.iframeAuto();
    /**
     * 物流信息渲染
     * @param shipBox
     * @param shipMsg
     */
    OrderInfoDlg.shipMsgRender=function(shipBox,shipMsg){
        if(shipMsg!=""&&shipMsg.length>0){
            for (var i=0;i<shipMsg.length;i++){
                var time = shipMsg[i].time;
                var status = shipMsg[i].status;
                shipBox.append('<div class="layui-colla-item">' +
                    '                            <h2 class="layui-colla-title">'+time+' </h2>' +
                    '                            <div class="layui-colla-content layui-show">'+status+'</div>' +
                    '                       </div>')
            }
        }else{
            shipBox.append('  <input    type="text" value="暂无物流信息" class="layui-input" disabled />')
        }

    }
    /**
     * 发货与未发货状态下的不同显示效果
     */
    OrderInfoDlg.shipChange=function(data){
        var shipBox = $("#shipBox");
        var shipBtn = $("#shipBtn");
        var b = data.orderStatus==30; //如果待发货
        shipBox.find("input").attr("disabled","disabled")
        if(b&&isAdmin){
            shipBtn.css("display",'block')
            shipBox.find("input").removeAttr("disabled")
        }
        shipBox.find("#shipTime").attr("disabled","disabled")
        shipBox.find("#handlePerson").attr("disabled","disabled")
        shipBox.find("#receiveTime").attr("disabled","disabled")
    };

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/order/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('orderForm', result.data);
    OrderInfoDlg.shipChange(result.data);
    if(result.data.orderStatus<30){
        $("#cacelBox").css("display","block")
    }
    if(result.data.shipNo!=""){
        $("#queryShipButton").css("display","block")
    }

    //渲染物流信息
    console.log(result.data.shipMsgList)
    OrderInfoDlg.shipMsgRender($("#shipNewMsg"),result.data.shipMsgList)
    /*if(result.data.orderStatus==40||result.data.orderStatus==50){
        $("#backingBox").css("display","block")
    }
    if(result.data.orderStatus==70){
        $("#backedBox").css("display","block")
    }*/

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/order/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set("orderStatus",$("#orderStatus").val())
        ajax.set(data.field);
       ajax.start();
    });

    //物流信息非空设置
    var notBlank= function(item,msg,ig){
        var flag=true;
        var find = item.find("input");
        find.each(function(){
            var val = $(this).val();
            var id = $(this).attr("id");
            if($.inArray(id, ig) ==-1 ){
                if(val==""){
                    Feng.error(msg);
                    flag= false;
                }
            }
        })
        return flag;
    }





    //快递表单提交事件
    form.on('submit(shipSubmit)', function (data) {
        var b = notBlank($("#shipBox"),"请正确填写物流信息!",["handlePerson","shipTime","receiveTime"]);
        if(!b){
            return b;
        }
            var ajax = new $ax(Feng.ctxPath + "/order/ship", function (data) {
                Feng.success("发货成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("发货失败！" + data.responseJSON.message)
            });
                ajax.set(data.field);
                ajax.start();
    });
    /**
     * 查看物流信息
     */




    //取消提交事件
    form.on('submit(cancelSubmit)', function (data) {
        var flag=false;
       /* var op=function(){
            flag=true;
            return false;
        }
       Feng.confirm("确定取消吗?",op)*/

        var ajax = new $ax(Feng.ctxPath + "/order/editItem", function (data) {
                Feng.success("取消成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("取消失败！" + data.responseJSON.message)
            });
            ajax.set(data.field);
            ajax.set("orderStatus","60");
            ajax.start();
    });


    /**
     * 退款按钮
     */
    OrderInfoDlg.submitAction=function(val){
        var ajax = new $ax(Feng.ctxPath + "/order/editItem", function (data) {
            Feng.success("操作成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("操作失败！" + data.responseJSON.message)
        });

        ajax.set(data.field);
        ajax.set("orderStatus",val);
        ajax.start();
    }

    $("#backingBox").click(function () {
        OrderInfoDlg.submitAction("70");
    })
    $("#backedBox").click(function () {
        OrderInfoDlg.submitAction("80");
    })

    var orderStatus = $("#orderStatus");
    if((orderStatus.val()=="10"||orderStatus.val()=="20")&&isAdmin){
        $("#totalGoodsPrice").parent().parent().css({"color":"#FF5722",});
        $("#shipFee").parent().parent().css({"color":"#FF5722",});
        $("#totalGoodsPrice").removeAttr("disabled")
        $("#shipFee").removeAttr("disabled")

    }



    laydate.render({
        elem: '#shipTime',
        type:'datetime',
    });


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderInfoDlg.tableId,
        url: Feng.ctxPath + '/order/goodsList?orderId='+$("#id").val(),
        page: false,
        cellMinWidth: 100,
        cols: OrderInfoDlg.initColumn()
    });

    /**
     * 点击查看详情
     * @param data
     */

    OrderInfoDlg.openGoodsDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        admin.open({
            type: 2,
            title: '商品信息',
            anim: 1,
            area:["100%","100%"],
            maxmin:'true',
            content: Feng.ctxPath + '/goods/goodsDetail?id=' + data.goodsId,
            end: function () {
                admin.getTempData('formOk') && table.reload(OrderInfoDlg.tableId);
            }
        });
    };
    // 工具条点击事件
    table.on('tool(' + OrderInfoDlg.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'goodsDetail') {
            OrderInfoDlg.openGoodsDetailDlg(data);
        }
    });
    //表格行双击
    table.on('rowDouble('+ OrderInfoDlg.tableId +')', function(obj){
        console.log(obj.tr) //得到当前行元素对象
        console.log(obj.data) //得到当前行数据
        //obj.del(); //删除当前行
        OrderInfoDlg.openGoodsDetailDlg(obj.data);
    });
});